package demo.reflect;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author zhanghaixuan
 * @date 2020-03-05 11:28
 */
@Slf4j
public class MethodCollector {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class<?> clazz = Class.forName("demo.reflect.ReflectTarget");

        final Method[] methods = clazz.getMethods();

        for (Method method : methods) {
            log.info("公有的方法:{}",method);
        }

        for (Method declaredMethod : clazz.getDeclaredMethods()) {
            log.info("所有的方法:{}",declaredMethod);
        }

        log.info("通过反射获取方法1,并执行");
        final Method show1 = clazz.getMethod("show1", String.class);
        final Constructor<?> constructor = clazz.getConstructor();
        final ReflectTarget reflectTarget = (ReflectTarget) constructor.newInstance();
        show1.invoke(reflectTarget,"反射方法执行1");

        log.info("通过反射获取私有方法1,并执行");
        final Method show4 = clazz.getDeclaredMethod("show4", int.class);
        show4.setAccessible(true);
        final Object show4Object = show4.invoke(reflectTarget, 1);
    }
}
